package test;

import sdljava.SDLException ;
import sdljava.joystick.* ;

public class Joystick {

    public static void main() throws SDLException {

        StringBuilder string = new StringBuilder();

        SDLJoystick stick = SDLJoystick.joystickOpen(0);

        while (true){
            string.setLength(0);
            SDLJoystick.joystickUpdate();
            int hats = stick.joystickNumHats();
            if (0 != hats){
                for (int hat = 0; hat < hats; hat++){
                    HatState state = stick.joystickGetHat(hat);
                    if (state.hatCentered()){
                        string.append('H');
                        string.append(hat);
                        string.append('C');
                    }
                    else if (state.hatUp()){
                        string.append('H');
                        string.append(hat);
                        string.append('U');
                    }
                    else if (state.hatRight()){
                        string.append('H');
                        string.append(hat);
                        string.append('R');
                    }
                    else if (state.hatDown()){
                        string.append('H');
                        string.append(hat);
                        string.append('D');
                    }
                    else if (state.hatLeft()){
                        string.append('H');
                        string.append(hat);
                        string.append('L');
                    }
                    else if (state.hatRightUp()){
                        string.append('H');
                        string.append(hat);
                        string.append("RU");
                    }
                    else if (state.hatRightDown()){
                        string.append('H');
                        string.append(hat);
                        string.append("RD");
                    }
                    else if (state.hatLeftUp()){
                        string.append('H');
                        string.append(hat);
                        string.append("LU");
                    }
                    else if (state.hatLeftDown()){
                        string.append('H');
                        string.append(hat);
                        string.append("LD");
                    }
                }
            }
            else {
                int buts = stick.joystickNumButtons();
                for (int but = 0; but < buts; but++){
                    if (stick.joystickGetButton(buts)){
                        string.append('B');
                        string.append(but);
                    }
                }
            }
            System.out.println(string);
        }
    }
}
